package com.huawei.hms.update.manager;

import android.content.Context;
import android.os.AsyncTask;
import com.huawei.hms.framework.network.restclient.hwhttp.HttpContants;
import com.huawei.hms.support.log.HMSLog;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.HttpsURLConnection;

public final class a {
    private static boolean a = false;
    private CountDownLatch b;

    class a extends AsyncTask<Context, Integer, Boolean> {
        final /* synthetic */ a a;
        private String b;

        protected /* synthetic */ Object doInBackground(Object[] objArr) {
            return a((Context[]) objArr);
        }

        public a(a aVar, String str) {
            this.a = aVar;
            this.b = str;
        }

        protected Boolean a(Context... contextArr) {
            boolean z = false;
            try {
                boolean z2;
                HttpsURLConnection httpsURLConnection = (HttpsURLConnection) new URL(this.b).openConnection();
                if (httpsURLConnection != null) {
                    httpsURLConnection.setRequestMethod(HttpContants.HTTP_METHOD_GET);
                    httpsURLConnection.setConnectTimeout(30000);
                    httpsURLConnection.setReadTimeout(30000);
                    httpsURLConnection.setUseCaches(false);
                    httpsURLConnection.setInstanceFollowRedirects(true);
                    HMSLog.i("PingTask", "GET google result:" + httpsURLConnection.getResponseCode());
                    z2 = true;
                } else {
                    z2 = false;
                }
                z = z2;
            } catch (RuntimeException e) {
                HMSLog.e("PingTask", "GET google result:RuntimeException");
            } catch (IOException e2) {
                HMSLog.e("PingTask", "GET google result:safe exception");
            } catch (Exception e3) {
                HMSLog.e("PingTask", "GET google result:Exception");
            }
            a.a = z;
            this.a.b.countDown();
            return Boolean.valueOf(z);
        }
    }

    public boolean a(long j, TimeUnit timeUnit, String str) {
        if (a) {
            HMSLog.i("PingTask", "ping google return cache");
            return true;
        }
        HMSLog.i("PingTask", "start ping goole");
        this.b = new CountDownLatch(1);
        new a(this, str).execute(new Context[0]);
        try {
            if (this.b.await(j, timeUnit)) {
                HMSLog.i("PingTask", "await:isReachable:" + a);
                return a;
            }
            HMSLog.i("PingTask", "await time out");
            return false;
        } catch (InterruptedException e) {
            HMSLog.e("PingTask", "await:InterruptedException:");
            return false;
        }
    }
}
